package com.lonelyzhe.springboot.service.impl;

import com.lonelyzhe.springboot.bean.OrderInfo;
import com.lonelyzhe.springboot.bean.OrderItem;
import com.lonelyzhe.springboot.mapper.OrderInfoMapper;
import com.lonelyzhe.springboot.service.IOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderInfoService implements IOrderInfoService {
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Override
    public List<OrderInfo> selectOrderList(int user_id) {
        return orderInfoMapper.getAllOrdersById(user_id);
    }
    public List<OrderInfo> selectOrderListWithoutPay(int user_id) {
        return orderInfoMapper.getAllOrdersByIdWithoutPay(user_id);
    }
    public List<OrderInfo> selectOrderListWithPay(int user_id) {
        return orderInfoMapper.getAllOrdersByIdWithPay(user_id);
    }
    public String updateToPay(int id) {
        String currentStatus = orderInfoMapper.getStatus(id);
        if(!currentStatus.equals("1")){
            return "failed";
        }
        orderInfoMapper.updateToPay(id);
        return "success";
    }
    public String updateToCancel(int id) {
        String currentStatus = orderInfoMapper.getStatus(id);
        if(!currentStatus.equals("1")){
            return "failed";
        }
        orderInfoMapper.updateToCancel(id);
        return "success";
    }
    public OrderInfo getOrderDetails(int id) {
        OrderInfo orderInfo = orderInfoMapper.getOrderDetails(id);
        List<OrderItem> orderItems = orderInfoMapper.getOrderItemsById(orderInfo.getId());
        orderInfo.setOrderItems(orderItems);
        return orderInfo;
    }
}
